require(['init'], function () {
    //获取当前的学生的
    var uid;
    var currentid;
    $.ajax({
        url: "/api/user/current",
        type: "get",
        async: false,
        success: function (data) {
            //console.log(data.data.userid)
            uid = data.data.userid;

        }
    })
    $.ajax({
        url: "/api/student/user/" + uid,
        type: "get",
        async: false,
        success: function (data) {
            currentid = data.data.studentuid;
        }
    })


    //班课即将上课
    function getmyclasseslist() {
        $.ajax({
            url: '/api/classe/myclasses/soon/' + currentid,
            method: 'get',
            success: function (data) {
                var $lesson_dl = $('#lesson_dl').html("");
                var $lesson_u = $("#lesson_u").html("");
                if (data.data.length == 0) {
                    var $nulldata = $('<div>' + i18next.t('table.nocurriculum') + '</div>').appendTo($lesson_dl);
                } else {
                    $('<dt><img src="' + (data.data.class.cover == null ? "../admin/img/icon_empty.png" : data.data.class.cover) + '"/></dt>\
           <dd>\
             <p>' + (data.data.class.classname == null ? "--" : data.data.class.classname) + '</p>\
             <p>' + i18next.t('table.keynoteteacher') + '：<span>' + (data.data.class.master == null ? "--" : data.data.class.master) + '</span></p>\
           </dd>').appendTo($lesson_dl);
                    var $lesson_li = $('<li>\
           <div></div>\
           <a>' + (data.data.schedule.name) + '</a><em style="margin-left:150px;font-style:normal">' + i18next.t('table.opentime') + ':' + data.data.schedule.begintime + '</em>\
           </li>').appendTo($lesson_u);
                    var $enroll_span = $('<span></span>').appendTo($lesson_li);
                    var scheduleid = data.data.schedule.scheduleid;
                    $.ajax({
                        url: "/api/schedule/" + scheduleid + "/wakeup",
                        method: "get",
                        data: {
                            type: 3,
                            userid: uid,
                        },
                        success: function (json) {
                            //开始时间
                            var ot = data.data.schedule.begintime.replace(/-/g, "/");
                            console.log(ot)
                            var classestime = (new Date(ot)).getTime() - (10 * 60 * 1000);
                            //结束时间
                            var et = data.data.schedule.endtime.replace(/-/g, "/");
                            var edtime = (new Date(et)).getTime();
                            //当前时间
                            var currenttime = new Date().getTime();
                            var intDiff = parseInt((classestime - currenttime)/1000);//倒计时总毫秒数
                            if (edtime < currenttime || currenttime >= classestime) {
                                var $enroll_a = $('<a href="' + json.data + '" style="color:#398dee"  target="_blank">' + i18next.t('timetable.goclassroom') + '</a>').appendTo($enroll_span);
                            } else {
                                var $enroll_a = $('<a>' + i18next.t('table.surplus') + ':<a id="day_show"></a>' + i18next.t('table.day') + '<a id="hour_show"></a>' + i18next.t('table.times') + '<a id="minute_show"></a>' + i18next.t('table.minute') + '<a id="second_show"></a>' + i18next.t('table.secondStart') + '</a>').appendTo($enroll_span);
                                var times=null;
                                function timers(intDiff) {
                                    times=setInterval(function () {
                                        var day = 0,
                                            hour = 0,
                                            minute = 0,
                                            second = 0;//时间默认值  
                                        if (intDiff > 0) {
                                            day = Math.floor(intDiff / 60 / 60 / 24);
                                            hour = Math.floor(intDiff / 60 / 60 - (24 * day));
                                            minute = Math.floor(intDiff / 60 - (24 * 60 * day) - (60 * hour));
                                            second = Math.floor(intDiff - (24 * 60 * 60 * day) - (60 * 60 * hour) - (60 * minute));
                                        }
                                        if (minute <= 9 && minute > 0) minute = '0' + minute;
                                        if (second <= 9 && second > 0) second = '0' + second;
                                        $('#day_show').html(day);
                                        $('#hour_show').html(hour);
                                        $('#minute_show').html(minute);
                                        $('#second_show').html(second);
                                        intDiff--;
                                    }, 1000);
                                }

                                $(function () {
                                    clearInterval(times);
                                    timers(intDiff);
                                });
                            }
                        }
                    })

                }
            }
        })
    }

    function table() {
        $(".s_ul_cour li").each(function () {
            var index = $(this).index();
            $(".s_ul_cour li").eq(0).addClass("li");
            $(this).click(function () {
                $(this).addClass("li").siblings().removeClass("li");
                $(".container").find('.stylesame').eq(index).stop(true).show().siblings().stop(true).hide();
            })
        })
    }

    function getcourse() {
        $.ajax({
            url: '/api/classe/myclasses/' + currentid,
            method: 'get',
            success: function (data) {
                readergetcourse(data)
            }
        })
    }

    //渲染页面
    function readergetcourse(data) {
        console.log(data);
        var $course_list = $(".course_list").html("")
        if (data.data.length == 0) {
            $('<div style="margin-left:19px;">' + i18next.t('table.nocurriculum') + '</div>').appendTo($course_list);
        } else {
            $.each(data.data, function (i, t) {
                console.log(t)
                var scheduleid = t.classuid;
                var $courselist = $('<div class="list">\
           <dl>\
             <dt><img src="' + (t.cover == null ? "../admin/img/icon_empty.png" : t.cover) + '"/></dt>\
             <dd>\
               <p>' + t.coursename + '</p>\
               <p>' + i18next.t('table.keynoteteacher') + '：<span>' + (t.master == null ? "--" : t.master) + ' </span></p>\
               <p style="display:none">' + i18next.t('table.nextcourse') + '：<span>心理学发展史（数据不好拿，先暂时隐藏该字段） </span></p>\
               <p>' + i18next.t('table.opentime') + '：<span>' + t.created_at + '</span></p>\
             </dd>\
           </dl>\
         </div>').appendTo($course_list)
                var $lesson = $('<div style="clear:both" class="informationlist"></div>').appendTo($course_list);
                var $lesson_class = $('<p class="p_lesson"><span class="c_le">' + i18next.t('table.open') + '</span><a>' + i18next.t('table.lessonlist') + '</a></p>').appendTo($lesson);
                var $ul = $('<ul class="ul_lesson" data-uid=' + scheduleid + ' style="display:none"></ul>').appendTo($lesson);
                //获取某个学生，某个课程下的所有课节
                $lesson_class.on('click', function () {
                    if ($ul.css("display") == "none") {
                        $lesson_class.find('span').html(i18next.t('table.packup'))//收起 v
                        $ul.show();
                        $.ajax({
                            url: '/api/classe/' + scheduleid + '/mylessons',
                            data: {
                                studentuid: currentid
                            },
                            method: 'get',
                            success: function (data) {
                                $ul.html("");
                                if (data.data.length == 0) {
                                    var $li = $('<li>' + i18next.t('table.nolesson') + '</li>').appendTo($ul);//该课程下没有课节
                                    return
                                }
                                $.each(data.data, function (index, item) {
                                    //开始时间
                                    var ot = item.begintime.replace(/-/g, "/");
                                    var classestime = (new Date(ot)).getTime() - (10 * 60 * 1000);
                                    //结束时间
                                    var et = item.endtime.replace(/-/g, "/");
                                    var edtime = (new Date(et)).getTime();
                                    var playbacktime = edtime + (30 * 60 * 1000)
                                    //当前时间
                                    var currenttime = new Date().getTime();
                                    var $li = $('<li>\
                        <div></div>\
                        <a style="display:inline-block;width:200px;">' + item.name + '</a><em style="font-style:normal">' + i18next.t('table.opentime') + ':' + item.begintime + '</em>\
                        </li>').appendTo($ul);
                                    var $span = $('<span></span>').appendTo($li);
                                    if (item.webcast == null && (currenttime >= edtime)) {
                                        var $a = $('<a>' + i18next.t('timetable.over') + '</a>').appendTo($span);
                                    } else if (item.webcast != null && currenttime > playbacktime) {
                                        var $a = $('<a href="' + item.webcast + '" style="color:#398dee" target="_blank">' + i18next.t('table.playback') + '</a>').appendTo($span);
                                    } else if ((currenttime >= classestime) || (edtime < currenttime)) {
                                        $.ajax({
                                            url: "/api/schedule/" + item.scheduleid + "/wakeup",
                                            method: "get",
                                            data: {
                                                type: 3,
                                                userid: uid//当前用户id
                                            },
                                            success: function (data) {
                                                var $a = $('<a href="' + data.data + '" style="color:#398dee" target="_blank">' + i18next.t('timetable.goclassroom') + '</a>').appendTo($span);
                                            }
                                        })
                                    }
                                    else if (classestime > currenttime) {
                                        var $a = $('<a style="color:#ccc">' + i18next.t('timetable.goclassroom') + '</a>').appendTo($span);
                                    }
                                })
                            }
                        })
                    } else {
                        $lesson_class.find('span').html(i18next.t('table.open'))//"打开 ^"
                        $ul.hide();
                    }
                })
            })
        }
    }

    //预约课程即将上课
    function getenrollmyclasseslist() {
        $.ajax({
            url: '/api/appoint/myclasses/soon/' + currentid,
            method: 'get',
            success: function (data) {
                var $lesson_dl = $('#enroll_lesson_dl').html("");
                var $lesson_u = $("#enroll_lesson_u").html("");
                if (data.data.length == 0) {
                    var $nulldata = $('<div>' + i18next.t('table.nocurriculum') + '</div>').appendTo($lesson_dl);
                } else {
                    $('<dt><img src="' + data.data.product.img + '"/></dt>\
        <dd>\
          <p>' + data.data.product.course + '</p>\
          <p>' + i18next.t('table.keynoteteacher') + '：<span>' + (data.data.product.teacher == null ? "--" : data.data.product.teacher) + '</span></p>\
        </dd>').appendTo($lesson_dl);
                    var $lesson_li = $('<li>\
        <div></div>\
        <a>' + data.data.schedule.name + '</a>\
        </li>').appendTo($lesson_u);
                    var $enroll_em = $('<em style="margin-left:150px;font-style:normal">' + i18next.t('table.opentime') + ':' + data.data.schedule.begintime + '</em>').appendTo($lesson_li);
                    var $enroll_span = $('<span></span>').appendTo($lesson_li);
                    $.ajax({
                        url: "/api/schedule/" + data.data.schedule.scheduleid + "/wakeup",
                        method: "get",
                        data: {
                            type: 3,
                            userid: uid
                        },
                        success: function (json) {
                            //开始时间
                            var ot = data.data.schedule.begintime.replace(/-/g, "/");
                            var classestime = ((new Date(ot)).getTime() - (10 * 60 * 1000));
                            //结束时间
                            var et = data.data.schedule.endtime.replace(/-/g, "/");
                            var edtime = (new Date(et)).getTime();
                            //当前时间
                            var currenttime = new Date().getTime();
                            console.log(classestime)
                            console.log(currenttime)
                            var intDiff = parseInt((classestime - currenttime)/1000);//倒计时总秒数量
                            if (edtime < currenttime || currenttime >= classestime) {
                                var $enroll_a = $('<a href="' + json.data + '" style="color:#398dee" target="_blank">' + i18next.t('timetable.goclassroom') + '</a>').appendTo($enroll_span);
                            } else {
                                var $enroll_a = $('<a>' + i18next.t('table.surplus') + ':<a id="day_show2"></a>' + i18next.t('table.day') + '<a id="hour_show2"></a>' + i18next.t('table.times') + '<a id="minute_show2"></a>' + i18next.t('table.minute') + '<a id="second_show2"></a>' + i18next.t('table.secondStart') + '</a>').appendTo($enroll_span);
                                var time;
                                function timer(intDiff) {
                                    time=setInterval(function () {
                                        var day = 0,
                                            hour = 0,
                                            minute = 0,
                                            second = 0;//时间默认值  
                                        if (intDiff > 0) {
                                            day = Math.floor(intDiff/60/60/24);
                                            hour = Math.floor(intDiff  / 60 / 60 - (24 * day));
                                            minute = Math.floor(intDiff  / 60 - (24 * 60 * day) - (60 * hour));
                                            second = Math.floor(intDiff  - (24 * 60 * 60 * day) - (60 * 60 * hour) - (60 * minute));
                                        }
                                        if (minute <= 9 && minute >= 0) minute = '0' + minute;
                                        if (second <= 9 && second >= 0) second = '0' + second;
                                        $('#day_show2').html(day);
                                        $('#hour_show2').html(hour);
                                        $('#minute_show2').html(minute);
                                        $('#second_show2').html(second);
                                        intDiff--;
                                    }, 1000);
                                }

                                $(function () {
                                    clearInterval(time);
                                    timer(intDiff);
                                });

                            }
                        }

                    })
                }
            }
        })
    }

    //预约课程列表
    function getenrcourse() { //api/appoint/myclasses/{id}
        $.ajax({
            url: '/api/appoint/myclasses/' + currentid,
            method: 'get',
            success: function (data) {
                readerernollgetcourse(data)
            }
        })
    }

    function readerernollgetcourse(data) {
        var $course_list = $(".enroll_course_list").html("");
        if (data.data.length == 0) {
            $('<div style="margin-left:19px;">' + i18next.t('table.nocurriculum') + '</div>').appendTo($course_list);
        } else {
            $.each(data.data, function (i, t) {
                var scheduleid = t.appoint.appointid;
                $.each(t.product, function (inde, ite) {
                    var $courselist = $('<div class="list">\
       <dl>\
         <dt><img src="' + ite.cover + '"/></dt>\
         <dd>\
           <p>' + ite.title + '</p>\
           <p>' + i18next.t('table.keynoteteacher') + '：<span>' + (ite.master == null ? "--" : ite.master) + ' </span></p>\
           <p style="display:none">' + i18next.t('table.nextcourse') + '：<span>心理学发展史（数据不好拿，先暂时隐藏该字段） </span></p>\
           <p>' + i18next.t('table.opentime') + '：<span>' + t.appoint.created_at + '</span></p>\
         </dd>\
       </dl>\
     </div>').appendTo($course_list)
                    var $lesson = $('<div style="clear:both" class="informationlist"></div>').appendTo($course_list);
                    var $lesson_class = $('<p class="p_lesson"><span class="c_le">' + i18next.t('table.open') + '</span><a>' + i18next.t('table.aboutlist') + '</a></p>').appendTo($lesson);
                    var $ul = $('<ul class="ul_lesson" data-uid=' + scheduleid + ' style="display:none"></ul>').appendTo($lesson);
                    //获取某个学生，某个课程下的所有课节
                    $lesson_class.on('click', function () {
                        if ($ul.css("display") == "none") {
                            $lesson_class.find('span').html(i18next.t('table.packup'))//"收起 v"
                            $ul.show();
                            $.ajax({
                                url: '/api/appointment/' + scheduleid + '/lessons',
                                data: {
                                    studentuid: currentid
                                },
                                method: 'get',
                                success: function (data) {
                                    $ul.html("");
                                    if (data.data.length != 0) {
                                        $.each(data.data, function (index, item) {
                                            //开始时间
                                            var ot = item.begintime.replace(/-/g, "/");
                                            var classestime = (new Date(ot)).getTime() - (10 * 60 * 1000);
                                            //结束时间
                                            var et = item.endtime.replace(/-/g, "/");
                                            var edtime = (new Date(et)).getTime();
                                            var playbacktime = edtime + (30 * 60 * 1000)
                                            //当前时间
                                            var currenttime = new Date().getTime();
                                            var $li = $('<li>\
                  <div></div>\
                  <a style="display:inline-block;width:200px;">' + item.name + '</a><em style="font-style:normal">' + i18next.t('table.opentime') + ':' + item.begintime + '</em>\
                  </li>').appendTo($ul);
                                            var $span = $('<span></span>').appendTo($li);
                                            if (item.webcast == null && (currenttime >= edtime)) {
                                                var $a = $('<a>' + i18next.t('timetable.over') + '</a>').appendTo($span);
                                            } else if (item.webcast != null && currenttime > playbacktime) {
                                                var $a = $('<a href="' + item.webcast + '" style="color:#398dee" target="_blank">' + i18next.t('table.playback') + '</a>').appendTo($span);
                                            } else if ((currenttime >= classestime) || (edtime < currenttime)) {
                                                $.ajax({
                                                    url: "/api/schedule/" + item.scheduleid + "/wakeup",
                                                    method: "get",
                                                    data: {
                                                        type: 3,
                                                        userid: uid//当前用户id
                                                    },
                                                    success: function (data) {
                                                        var $a = $('<a href="' + data.data + '" style="color:#398dee" target="_blank" >' + i18next.t('timetable.goclassroom') + '</a>').appendTo($span);
                                                    }
                                                })
                                            }
                                            else if (classestime > currenttime) {
                                                var $a = $('<a style="color:#ccc">' + i18next.t('timetable.goclassroom') + '</a>').appendTo($span);
                                            }
                                        })
                                    } else {
                                        $('<li>' + i18next.t('table.noorder') + '</li>').appendTo($ul);//该课程下还没有预约哦!
                                    }
                                }
                            })
                        } else {
                            $lesson_class.find('span').html(i18next.t('table.open'));//"打开 ^"
                            $ul.hide();
                        }
                    })
                })
            })
        }
    }

    function getall() {
        table()//table 切换
        getmyclasseslist()//班课即将上课
        getcourse()//班课的课程列表
        getenrollmyclasseslist()//预约课程即将上课
        getenrcourse()//预约课下的约课列表
    }

    getall()
})
